package co.edu.unal.satacredito.mediation;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import co.edu.unal.satacredito.database.administration.ejb.BrandDaoEJBLocal;
import co.edu.unal.satacredito.database.administration.exceptions.DAOException;
import co.edu.unal.satacredito.database.entity.Brand;
import co.edu.unal.satacredito.infrastructure.dto.BrandDTO;

/**
 * Session Bean implementation class BrandEJB
 */
@Stateless
public class BrandEJB implements BrandEJBRemote, BrandEJBLocal {

	@EJB
	private BrandDaoEJBLocal brandDaoEJBLocal;
    
    public BrandEJB() {
    	
    }
    
    @Override
    public List<BrandDTO> findAllBrands(){
		try {
			List<Brand> brands = brandDaoEJBLocal.findAllBrands();
			List<BrandDTO> brandDTO = new ArrayList<BrandDTO>();
			
			for (Brand brand : brands) {
				brandDTO.add(new BrandDTO(brand));
			}
			
			return brandDTO;
			
		} catch (DAOException e) {
			return new ArrayList<BrandDTO>();
		}
    }
    
    @Override
    public BrandDTO findBrand(String brandId) throws Exception{
    	try {
    		Brand brand = new Brand();
    		brand = brandDaoEJBLocal.findBrandById(brandId);
			BrandDTO brandDTO = new BrandDTO(brand);
			return brandDTO;
			
		} catch (Exception e) {
			return null;
		}
    }
    
    @Override
    public void create(BrandDTO brandDTO) throws Exception{
    	try {
    		Brand brand = new Brand();
    		brand.setBrandName(brandDTO.getBrandName());
    		brand.setNit(brandDTO.getNit());
    		brand.setCEOFirstName(brandDTO.getCEOFirstName());
    		brand.setCEOLastName(brandDTO.getCEOLastName());
    		brand.setCEODocument(brandDTO.getCEODocument());
    		brand.setEmail(brandDTO.getEmail());
    		brand.setPhoneNumber(brandDTO.getPhoneNumber());
    		brand.setStatus("enable");
			brandDaoEJBLocal.createBrand(brand);
		} catch (Exception e) {
			throw e;
		}
    }
    
    @Override
    public void edit(String id, BrandDTO brandDTO) throws Exception{
    	try {
    		Brand brand = new Brand();
    		brand.setBrandName(brandDTO.getBrandName());
    		brand.setNit(brandDTO.getNit());
    		brand.setCEOFirstName(brandDTO.getCEOFirstName());
    		brand.setCEOLastName(brandDTO.getCEOLastName());
    		brand.setCEODocument(brandDTO.getCEODocument());
    		brand.setEmail(brandDTO.getEmail());
    		brand.setPhoneNumber(brandDTO.getPhoneNumber());
    		brand.setStatus(brandDTO.getStatus());
			brandDaoEJBLocal.editBrand(id, brand);
		} catch (Exception e) {
			throw e;
		}
    }
    
    @Override
    public void delete(String id) throws Exception{
    	try {
			brandDaoEJBLocal.deleteBrand(id);
		} catch (Exception e) {
			throw e;
		}
    }

}
